package cn.wzy.service.impl;

import cn.wzy.results.ResponseResult;
import cn.wzy.service.FastDfsService;
import cn.wzy.utils.FastdfsUtil;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class FastDfsServiceImpl implements FastDfsService {
    @Value("${fastdfs.url}")
    private String fastDfsUrl;
    @Override
    public ResponseResult upload(MultipartFile file) {
        try {
            byte[] bytes = file.getBytes();
            String filename = file.getOriginalFilename();
            String extension = FilenameUtils.getExtension(filename);
            String path = FastdfsUtil.upload(bytes, extension);
            return ResponseResult.okResult(fastDfsUrl+path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResponseResult.errorResult(500,"上传失败");
    }

    @Override
    public ResponseResult delete(String path) {
        path = path.substring(fastDfsUrl.length());
        FastdfsUtil.delete(path);
        return ResponseResult.okResult();
    }
}
